<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Config extends Model
{
    protected $table = 'config';
    protected $primaryKey = 'id';
    public $timestamps = false;
    /**
     * @id 查询配置的id
     *以key=>value形式返回config中的配置参数
     * @return array(
     *          1=>'普通红包',
     *          2=>'利息红包'
     *          )
     */
    public static function get_config($id){
        if($cache = cache('config.id.'.$id)){ return $cache; }

        $data = DB::table('config')->select('value')->where('id', '=', $id)->first();
        if ($data == NULL) {
            $config=array();
        }else{
            if ($data->value=="") {
                $config=array();
            }else{
                $config=String2Array($data->value);
            }
        }
       
        cache(['config.id.'.$id => $config], 43200);
        return $config;
    } 

    /**
     * @key 查询配置的key值
     *以key=>value形式返回config中的配置参数
     * @return array(
     *          1=>'普通红包',
     *          2=>'利息红包'
     *          )
     */
    public static function get_config_bykey($key){
        if($cache = cache('config.key.'.$key)){ return $cache; }

        $data = DB::table('config')->select('value')->where('key', '=', $key)->first();
        if ($data == NULL) {
            $config=array();
        }else{
            if ($data->value=="") {
                $config=array();
            }else{
                $config=String2Array($data->value);
            }
        }

        cache(['config.key.'.$key => $config], 43200);
        return $config;
    } 

}
